<template>
  <div class="unauthorized-page">
    <div class="decoration">
      <div class="hexagon hexagon-1"></div>
      <div class="hexagon hexagon-2"></div>
      <div class="hexagon hexagon-3"></div>
      <div class="dot" v-for="i in 3" :key="i"></div>
    </div>

    <div class="container" ref="containerRef">
      <div class="user-icon">👤</div>
      <div class="error-code">401</div>
      <h1 class="error-title">未认证</h1>
      <p class="error-message">
        您需要登录才能访问此页面。<br />
        请先登录或注册账户。
      </p>
      <div class="btn-group">
        <button class="btn login-btn" @click="handleLogin" :disabled="isRedirecting">
          {{ loginBtnText }}
        </button>
        <button class="btn home-btn" @click="goHome" :disabled="isRedirecting">
          {{ homeBtnText }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import { useRouter } from 'vue-router';

// 响应式数据
const containerRef = ref(null);
const isRedirecting = ref(false);
const loginBtnText = ref('立即登录');
const homeBtnText = ref('返回');
const router = useRouter();

// 鼠标移动视差效果
const handleMouseMove = (e) => {
  if (!containerRef.value) return;
  const x = (window.innerWidth - e.pageX) / 200;
  const y = (window.innerHeight - e.pageY) / 200;
  containerRef.value.style.transform = `translate(${x}px, ${y}px)`;
};

// 登录处理
const handleLogin = () => {
  isRedirecting.value = true;
  loginBtnText.value = '正在跳转...';

  setTimeout(() => {
    router.push('/auth');
  }, 800);
};

// 返回首页
const goHome = () => {
  isRedirecting.value = true;
  homeBtnText.value = '正在返回...';

  setTimeout(() => {
    router.back();
  }, 800);
};

// 检测是否为触摸设备
const isTouchDevice = () => {
  return 'ontouchstart' in window || navigator.maxTouchPoints > 0;
};

// 组件挂载
onMounted(() => {
  if (!isTouchDevice()) {
    window.addEventListener('mousemove', handleMouseMove);
  }
});

// 组件卸载
onUnmounted(() => {
  window.removeEventListener('mousemove', handleMouseMove);
});
</script>

<style lang="scss" scoped>
.unauthorized-page {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
    Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  overflow: hidden;
  position: relative;
  padding: 20px;
  background: linear-gradient(135deg, #141e30, #243b55);

  .container {
    text-align: center;
    z-index: 10;
    padding: 1.5rem;
    background: rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(10px);
    border-radius: 20px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
    border: 1px solid rgba(255, 255, 255, 0.1);
    width: 100%;
    max-width: 400px;
    position: relative;
    transition: transform 0.1s ease;

    .user-icon {
      font-size: 3rem;
      margin-bottom: 1rem;
      color: #00d2ff;
      animation: user-pulse 2s infinite;
    }

    .error-code {
      font-size: 8rem;
      font-weight: 800;
      color: rgba(255, 255, 255, 0.8);
      text-shadow: 0 0 15px rgba(0, 200, 255, 0.6);
      margin-bottom: -1rem;
      position: relative;
      animation: blink 2s infinite;
      line-height: 1;

      &::before {
        content: "401";
        position: absolute;
        top: 0;
        left: 0;
        color: transparent;
        text-shadow: 0 0 20px rgba(0, 200, 255, 0.8);
        z-index: -1;
        animation: pulse 3s infinite;
      }
    }

    .error-title {
      font-size: 1.8rem;
      color: #00d2ff;
      margin-bottom: 1rem;
      text-transform: uppercase;
      letter-spacing: 2px;
      font-weight: 600;
    }

    .error-message {
      font-size: 1rem;
      color: rgba(255, 255, 255, 0.8);
      margin-bottom: 1.5rem;
      line-height: 1.6;
    }

    .btn-group {
      display: flex;
      gap: 10px;
      justify-content: center;
      flex-wrap: wrap;

      .btn {
        padding: 12px 20px;
        color: white;
        text-decoration: none;
        border-radius: 50px;
        font-weight: 600;
        letter-spacing: 1px;
        transition: all 0.3s ease;
        border: none;
        cursor: pointer;
        font-size: 0.9rem;
        min-width: 120px;

        &:hover:not(:disabled) {
          transform: translateY(-2px);
        }

        &:active:not(:disabled) {
          transform: translateY(1px);
        }

        &:disabled {
          opacity: 0.7;
          cursor: not-allowed;
        }
      }

      .login-btn {
        background: linear-gradient(45deg, #00c6ff, #0072ff);
        box-shadow: 0 4px 15px rgba(0, 114, 255, 0.4);

        &:hover:not(:disabled) {
          box-shadow: 0 6px 20px rgba(0, 114, 255, 0.6);
        }
      }

      .home-btn {
        background: linear-gradient(45deg, #654ea3, #da8443);
        box-shadow: 0 4px 15px rgba(101, 78, 163, 0.4);

        &:hover:not(:disabled) {
          box-shadow: 0 6px 20px rgba(101, 78, 163, 0.6);
        }
      }
    }
  }

  .decoration {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    pointer-events: none;

    .hexagon {
      position: absolute;
      clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
      background: linear-gradient(45deg, rgba(0, 200, 255, 0.1), transparent);

      &-1 {
        width: 120px;
        height: 120px;
        top: -60px;
        right: -60px;
      }

      &-2 {
        width: 90px;
        height: 90px;
        bottom: -45px;
        left: -45px;
      }

      &-3 {
        width: 70px;
        height: 70px;
        top: 25%;
        left: 8%;
      }
    }

    .dot {
      position: absolute;
      width: 8px;
      height: 8px;
      border-radius: 50%;
      background: rgba(0, 200, 255, 0.3);
      animation: dot-float 5s infinite ease-in-out;

      &:nth-child(1) {
        top: 15%;
        left: 10%;
        animation-delay: 0s;
      }

      &:nth-child(2) {
        top: 60%;
        right: 12%;
        animation-delay: 1.5s;
      }

      &:nth-child(3) {
        bottom: 30%;
        left: 18%;
        animation-delay: 3s;
      }
    }
  }

  // 动画定义
  @keyframes blink {

    0%,
    100% {
      opacity: 1;
    }

    50% {
      opacity: 0.6;
    }
  }

  @keyframes pulse {

    0%,
    100% {
      opacity: 0.8;
      transform: scale(1);
    }

    50% {
      opacity: 0.4;
      transform: scale(1.05);
    }
  }

  @keyframes user-pulse {

    0%,
    100% {
      transform: scale(1);
    }

    50% {
      transform: scale(1.1);
    }
  }

  @keyframes dot-float {
    0% {
      transform: translateY(0) translateX(0);
      opacity: 0.3;
    }

    25% {
      transform: translateY(-20px) translateX(10px);
      opacity: 0.7;
    }

    50% {
      transform: translateY(-40px) translateX(-10px);
      opacity: 0.3;
    }

    75% {
      transform: translateY(-20px) translateX(-20px);
      opacity: 0.7;
    }

    100% {
      transform: translateY(0) translateX(0);
      opacity: 0.3;
    }
  }

  // 响应式设计
  @media (max-width: 480px) {
    .container {
      padding: 1.2rem;

      .error-code {
        font-size: 7rem;
      }

      .error-title {
        font-size: 1.5rem;
      }

      .error-message {
        font-size: 0.9rem;
      }

      .btn-group {
        flex-direction: column;
        align-items: center;

        .btn {
          width: 100%;
          max-width: 200px;
        }
      }
    }
  }

  @media (max-width: 360px) {
    .container {
      .error-code {
        font-size: 6rem;
      }

      .error-title {
        font-size: 1.3rem;
      }
    }
  }
}
</style>
